package com.library.action;

import com.library.model.Userinfo;
import com.library.service.UserService;

public class UserAction extends BaseAction{
	private UserService userService;
	private Userinfo user;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public Userinfo getUser() {
		return user;
	}

	public void setUser(Userinfo user) {
		this.user = user;
	}

	public String toLogin() throws Exception{
		return "log";
	}
	public String doLogout() throws Exception{
		request.getSession().invalidate();
		response.sendRedirect("allBook!toBookList.action");
		return null;
	}
	public String doLogin() throws Exception{
		String username = user.getUsername();
		String userpwd = user.getUserpwd();
		//user = userService.getUser(username, userpwd);
		int roleid = user.getRoleinfo().getRoleid();
		user = userService.checkUser(user.getUsername(), user.getUserpwd(), roleid);
		if(user!=null){
			if(roleid == 1){
				session.put("USER", user);
				response.sendRedirect("adminBook!toAdminBookList.action");				
			}else{
				session.put("USER", user);
				response.sendRedirect("allBook!toBookList.action");
			}			
			return null;
		}else{
			return "log";
		}
	}
}
